package com.letfox.frm.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.letfox.common.model.ResultResponse;
import com.letfox.common.util.JacksonUtil;
import com.letfox.frm.model.FRMFile;
import com.letfox.frm.service.FRMFileService;
import com.letfox.frm.service.UploadService;

@Service
@Transactional
public class FRMUploadFileImpl implements UploadService {

	@Autowired
	private FRMFileService frmFileService;

	@Override
	public ResponseEntity<byte[]> downloadFile(String path) throws Exception {
		//获取文件流
		File file=new File(path);		
		//文件名字
		String type = file.getName();
		HttpHeaders headers=new HttpHeaders();
		//设置下载文件名，编码
        String fileName=new String(type.getBytes("UTF-8"),"iso-8859-1");
        headers.setContentDispositionFormData("attachment", fileName);   
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //下载文件
		ResponseEntity<byte[]> responseEntity=new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);	
		return responseEntity;
	}

	@Override
	public ResultResponse uploadFile(MultipartFile[] filter, String path)throws Exception {
		ResultResponse resultResponse=new ResultResponse();
		List<String> jsonList=new ArrayList<String>();
		for (MultipartFile multipartFile : filter) {
			if(!multipartFile.isEmpty()){
				//文件重命名
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				String rename=(df.format(new Date())).replace("-","").replace(":", "").replace(" ", "");
				String[] strs= multipartFile.getOriginalFilename().split("\\.");
			    String type=strs[1];
				String fileNamne=rename+"."+type;
				//-----添加数据------
				FRMFile fFrmFile = new FRMFile();
				// 文件唯一编码
				fFrmFile.setFileId(UUID.randomUUID().toString());
				// 得到文件名
				fFrmFile.setFileName(fileNamne);
				// 文件路径
				fFrmFile.setFileUrl(path);
				// 文件类型
				fFrmFile.setFileType(type);
				// 文件大小
				fFrmFile.setFileSize(multipartFile.getSize() + "");
				// 创建时间
				fFrmFile.setCreateTime(new Date());
				// 修改时间
				fFrmFile.setUpdateTime(new Date());
				fFrmFile.setDel("0");								
				// 添加数据库
				frmFileService.save(fFrmFile);
				//写入文件数据
				FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(path, fileNamne)); 
				String jsonPath=path +  fileNamne;
				jsonList.add(jsonPath);
			}
		}
		if(jsonList.size()>0){
			resultResponse.setData(JacksonUtil.toJson(jsonList));
		}
		return resultResponse;

	}
	
	


}
